package com.doge.service.wx.user;

import com.doge.bean.base.BaseRespVo;
import com.doge.bean.pojo.order.Order;
import com.doge.bean.pojo.order.OrderExample;
import com.doge.bean.pojo.order.OrderGoods;
import com.doge.bean.pojo.order.OrderGoodsExample;
import com.doge.bean.pojo.user.User;
import com.doge.bean.vo.user.UserIndexVO;
import com.doge.mapper.CommentMapper;
import com.doge.mapper.OrderGoodsMapper;
import com.doge.mapper.OrderMapper;
import com.doge.util.auth.GetSubject;
import com.doge.mapper.SystemMapper;
import com.doge.service.wx.order.WxOrderServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: project2-wechat-mall
 * @description: 用户实现类
 * @author: zhu lei
 * @create: 2021-12-02 10:17
 **/
@Service
public class WxUserServiceImpl implements WxUserService {
    @Autowired
    CommentMapper commentMapper;
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OrderGoodsMapper orderGoodsMapper;
    @Autowired
    SystemMapper systemMapper;
    @Autowired
    WxOrderServiceImpl wxOrderService;


    @Override
    public UserIndexVO getUserIndex() {

        // 自动判断订单是否符合后台配置的代码

        // 查询该user的订单
        OrderExample orderExample1 = new OrderExample();
        orderExample1.createCriteria().andUserIdEqualTo(GetSubject.getUser().getId()).andDeletedEqualTo(false);
        List<Order> userOrders = orderMapper.selectByExample(orderExample1);

        // 获得订单的配置
        // market_order_unpaid         用户下单后超时、用户未付款，则订单自动取消
        // market_order_unconfirm   订单发货后超期，未确认收货，则订单自动确认收货
        // market_order_comment    确认收货后超期、未评价商品，则取消评价资格
        Integer market_order_unpaid = Integer.valueOf(systemMapper.getMarketSystemByKeyName("market_order_unpaid"));
        Integer market_order_unconfirm = Integer.valueOf(systemMapper.getMarketSystemByKeyName("market_order_unconfirm"));
        Integer market_order_comment = Integer.valueOf(systemMapper.getMarketSystemByKeyName("market_order_comment"));

        Integer unpayOverTime = market_order_unpaid * 60000;
        Integer unconfirmOverTime = market_order_unconfirm * 86400000;
        Integer uncommentOverTime = market_order_comment * 86400000;

        for (Order order1 : userOrders) {

            Short orderStatus = order1.getOrderStatus();

            if (orderStatus == 101) {
                // 未付款
                long addTime = order1.getAddTime().getTime();
                long nowTime = System.currentTimeMillis();

                if (nowTime - addTime > unpayOverTime) {
                    // 要改为103:系统取消
                    wxOrderService.orderChangeStatus(order1.getId(), 103);
                }

            } else if (orderStatus == 301) {
                // 已发货
                long updateTime = order1.getUpdateTime().getTime();
                long nowTime = System.currentTimeMillis();

                if (nowTime - updateTime > unconfirmOverTime) {
                    // 要改为402:系统收货
                    wxOrderService.orderChangeStatus(order1.getId(), 402);
                }
            } else if (orderStatus == 401 || orderStatus == 402) {
                // 已收货
                // 查询该订单id的全部未评价orderGoods
                OrderGoodsExample orderGoodsExample = new OrderGoodsExample();
                orderGoodsExample.createCriteria().andOrderIdEqualTo(order1.getId()).andCommentEqualTo(0);
                List<OrderGoods> orderGoods = orderGoodsMapper.selectByExample(orderGoodsExample);


                for (OrderGoods orderGood : orderGoods) {

                    long updateTime = orderGood.getUpdateTime().getTime();
                    long nowTime = System.currentTimeMillis();

                    if (nowTime - updateTime > uncommentOverTime) {
                        // comment要改为-1
                        orderGood.setComment(-1);

                        orderGoodsMapper.updateByPrimaryKey(orderGood);
                    }
                }
            }
        }

        User user = GetSubject.getUser();
//        先通过用户id找到用户的所有订单,查询Order表
        OrderExample orderExample = new OrderExample();

        // 筛选订单的状态是401用户收货、402系统收货
        List<Short> shortList = new ArrayList<>();
        shortList.add((short) 401);
        shortList.add((short) 402);

        orderExample.createCriteria().andUserIdEqualTo(user.getId()).andOrderStatusIn(shortList).andDeletedEqualTo(false);

        List<Order> orders = orderMapper.selectByExample(orderExample);

        Integer uncommentTotal = new Integer(0);
        for (Order order : orders) {

//        通过订单id找到该订单的未评论商品的数目,查询OrderGoods表
            OrderGoodsExample orderGoodsExample = new OrderGoodsExample();
            orderGoodsExample.createCriteria().andOrderIdEqualTo(order.getId()).andCommentEqualTo(0);

            //Integer uncomment = orderGoodsMapper.unCommentByOrderId(order.getId());
            List<OrderGoods> orderGoods = orderGoodsMapper.selectByExample(orderGoodsExample);

            uncommentTotal += orderGoods.size();
        }

//        通过用户id找order订单里面未付款的商品数量
        Integer unpaid = orderMapper.unPaidByUser(user);
//        通过用户id找order订单里面待收货的商品数量
        Integer unrecv = orderMapper.unRecvByUser(user);
//        通过用户id找order订单里面代发货的商品数量
        Integer unship = orderMapper.unShipByUser(user);
//        分别初始化vo类和order类
        UserIndexVO userIndexVO = new UserIndexVO();
        UserIndexVO.OrderDTO orderDTO = new UserIndexVO.OrderDTO();

//        对order类进行赋值
        orderDTO.setUncomment(uncommentTotal);
        orderDTO.setUnpaid(unpaid);
        orderDTO.setUnrecv(unrecv);
        orderDTO.setUnship(unship);
//        对vo类的order进行赋值
        userIndexVO.setOrder(orderDTO);
        return userIndexVO;
    }
}
